// pages/settleIn/index.js
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    product:{
      name: "",
      isShare: 0,
      image: "",
      smallImage: "",
      detailImage: "",

    },
    typeIdIndex:-1,
    specificationsAtrr:[]
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    wx.hideShareMenu()
    if(options.id){
      this.setData({
        id:parseInt(options.id)
      })
      wx.setNavigationBarTitle({
        title: '编辑产品',
      })
    }else{
      wx.setNavigationBarTitle({
        title: '上传产品',
      })
    }
    this.typeList()
    
  },
  
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  },

  typeList(){
    var that=this
  
      var data = {
        id:wx.getStorageSync('userInfo').storeId
      }
      app.agriknow.storeProTypeList(data).then(res=>{
        var datas=res.data
        this.setData({
          typeList:datas,
        })
        if(datas.length==0){
          wx.showModal({
            title: '温馨提示',
            content: '请先添加产品分类',
            showCancel:false,
            complete: (res) => {
              if (res.confirm) {
                wx.navigateTo({
                  url: '/pages/productManagement/index?tabIndex=2',
                })
              }
            }
          })
          return false
        }
        var id=that.data.id
        if(id){
          that.getProductDetail()
        }
        
       
      })
   
  },
  getProductDetail:function(){
    var that=this
    var data={}
    var id=this.data.id
    data.id=id
    app.agriknow.commRequest('ht/store/proUpdDetails',data).then(res=>{
      var datas=res.data
      var typeList=that.data.typeList
      var typeIdIndex=typeList.findIndex(item=>{return item.typeId=datas.typeId})
      console.log(typeIdIndex)
      var specificationsAtrr=[]
      if(datas.specifications){
        specificationsAtrr=datas.specifications.split(',')
      }
      that.setData({
        product:datas,
        typeIdIndex:typeIdIndex,
        specificationsAtrr:specificationsAtrr
      })
    })
  },

  input:function(e){
      var key='product.'+e.currentTarget.dataset.key
     
      //console.log(e)
      this.setData({
        [key]:e.detail.value
      })

  },
  pickerChange:function(e){
    console.log(e)
    var val=e.detail.value
    if(val==-1){
      return false
    }
    var name=e.currentTarget.dataset.name
    var typeList=this.data.typeList
    var key=name+'Index'
    var key2='product.'+name
    this.setData({
        [key]:val,
        [key2]:typeList[val].id
    })
  },

  uploadImg(e) {
    let key = 'product.'+e.currentTarget.dataset.name
    let that = this
    wx.chooseMedia({
      count:1,
      success (res) {
        console.log(res.tempFiles[0].tempFilePath)
        const tempFilePaths = res.tempFiles[0].tempFilePath
        wx.uploadFile({
          url: app.agriknow._baseUrl+'ht/user/uploadImage', //仅为示例，非真实的接口地址
          filePath: tempFilePaths,
          name: 'file',
          formData: {
            'user': 'test'
          },
          success (res){
            that.setData({
              [key]:JSON.parse(res.data).data
            })
            
          }
        })
      }
    })
  },
  selectShare:function(e){
      var share=e.currentTarget.dataset.share
      var key='product.isShare'
      this.setData({
        [key]:share
      })
  },
  formSubmit:function(){
    var data={}
    data=this.data.product
    var tips=''
    console.log(data.stockNum)
    if(!data.name){
        tips='请输入产品名称'
    }else if(!data.price){
        tips='请输入产品价格'
    }else if(!data.costPrice){
      tips='请输入供应价格'
    }else if(Number(data.price)<Number(data.costPrice)){
      tips='产品价格不能低于供应价'
    }else if(data.stockNum==undefined){
      tips='请输入产品库存数量'
    }else if(!data.typeId){
      tips='请选择产品分类'
    }else if(!data.specifications){
      tips='请输入规格'
    }else if(!data.describe){
      tips='请输入产品描述'
    }else if(!data.smallImage){
      tips='请上传产品缩略图'
    }else if(!data.image){
      tips='请上传产品主图'
    }else if(!data.detailImage){
      tips='请上传产品详情图'
    }
    if(tips){
      wx.showToast({
        title: tips,
        icon:'none',
        duration:1500,
        mask:true
      })
      return false
    }
    if(data.specifications){
      data.specifications=data.specifications.replace(/，/g, ',').replace(/\s/g, "")
    }
    data.storeId=wx.getStorageSync('userInfo').storeId
    var id=this.data.id
    var url=''
    var mgs=''
    if(id){
      data.id=id
      url='/ht/store/storeUpdPro'
      mgs='编辑'

    }else{
      url='/ht/store/storeAddPro'
      mgs='添加'
    }
    app.agriknow.commRequest(url,data).then(res=>{
      if(res.code=='000000'){
        wx.showToast({
          title: mgs+'成功',
          icon:'none',
          duration:1500,
          mask:true
        })
        setTimeout(function(){
          wx.navigateBack()
        },1500)
      }else{
        wx.showToast({
          title: mgs+'失败',
          icon:'none',
          duration:1500,
          mask:true
        })
      }
    })
  }
})